package com.example.valueanimationhelper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements ValueAnimatorHelper.AnimationListener {

    private LinearLayoutManager linearLayoutManager;
    private RecyclerView recyclerView;
    private ImageView duplicateViewClicked;
    private ValueAnimatorHelper valueAnimatorHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        valueAnimatorHelper = new ValueAnimatorHelper(this, this);

        duplicateViewClicked = findViewById(R.id.img_clicked);
        recyclerView = findViewById(R.id.recycler_view);
		
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        DemoRecyclerViewAdapter adapter = new DemoRecyclerViewAdapter();
        adapter.setItemClickListener(new DemoRecyclerViewAdapter.ItemClickListener() {
            @Override
            public void setOnItemClickListener(View itemView, int position) {
                Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
                View viewByPosition = linearLayoutManager.findViewByPosition(position);
                int left = viewByPosition.getLeft();
                int top = recyclerView.getTop();

                duplicateViewClicked.setVisibility(View.INVISIBLE);
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) duplicateViewClicked.getLayoutParams();
                params.setMargins(left, top, 0, 0);
                duplicateViewClicked.setLayoutParams(params);

                startCheckedItemAnimation(left, top);
            }
        });
        recyclerView.setAdapter(adapter);


        recyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                //RecyclerView列表整体滑动
                int initPaddingLeft = recyclerView.getPaddingLeft();

                int distance = getResources().getDisplayMetrics().widthPixels;
                recyclerView.setPadding(initPaddingLeft + distance, 0, 0, 0);
                recyclerView.scrollBy(-distance, 0);

                //滑动后的padding
                int paddingLeftChanged = recyclerView.getPaddingLeft();
                int targetPaddingLeft = paddingLeftChanged - distance;

                ArrayList<ValueAnimatorHelper.ValueContainer> list = new ArrayList<>();
                ValueAnimatorHelper.ValueContainer valueContainer = new ValueAnimatorHelper
                        .ValueContainer(300, 1000, "TEST",
                        paddingLeftChanged, 0);
                ValueAnimatorHelper.ValueContainer valueContainer2 = new ValueAnimatorHelper
                        .ValueContainer(80, 1300, "TEST",
                        0, targetPaddingLeft);
                list.add(valueContainer);
                list.add(valueContainer2);
                valueAnimatorHelper.playMultipleAnimations(list);
            }
        }, 0);
    }

    private void startCheckedItemAnimation(int left, int top) {
        int targetLeft = getResources().getDisplayMetrics().widthPixels / 2 - duplicateViewClicked.getWidth() / 2;
        int targetTop = getResources().getDisplayMetrics().heightPixels / 2 - duplicateViewClicked.getHeight() / 2;

        ValueAnimatorHelper.AnimationValue startValue = new ValueAnimatorHelper.AnimationValue(left, top, 1.0f);
        ValueAnimatorHelper.AnimationValue endValue = new ValueAnimatorHelper.AnimationValue(targetLeft, targetTop, 2.0f);

        ValueAnimatorHelper.ValueContainer valueContainer = new ValueAnimatorHelper
                .ValueContainer(200, 0,
                "CLICK_ITEM", startValue, endValue);
        valueAnimatorHelper.playSingleAnimation(valueContainer);
    }


    @Override
    public void onAnimationStart(float value, String propertyName) {
        if ("CLICK_ITEM".equals(propertyName)) {
            duplicateViewClicked.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onAnimationEnd(float value, String propertyName) {
        if ("TEST".equals(propertyName)) {
            recyclerView.setPadding((int) value, 0, 0, 0);
            recyclerView.scrollBy((int) -value, 0);
        }
    }

    @Override
    public void updateValue(float value, String propertyName) {
        if ("TEST".equals(propertyName)) {
            recyclerView.setPadding((int) value, 0, 0, 0);
        }
    }

    @Override
    public void updateAnimationValue(ValueAnimatorHelper.AnimationValue
                                             animationValue, String propertyName) {
        if ("CLICK_ITEM".equals(propertyName)) {
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) duplicateViewClicked.getLayoutParams();
            params.setMargins((int) animationValue.value1, (int) animationValue.value2, 0, 0);
            duplicateViewClicked.setLayoutParams(params);
            duplicateViewClicked.setScaleX(animationValue.value3);
            duplicateViewClicked.setScaleY(animationValue.value3);
        }
    }
}
